package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.strategy.vo.CatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 攻略分类Service业务层处理
 *
 * @author dalang
 * @date 2023-11-23
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper, StrategyCatalog> implements IStrategyCatalogService {
    @Autowired
    private StrategyServiceImpl strategyService;

    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    public List<CatalogVO> groupList0() {
        List<StrategyCatalog> StrategyCatalogs = lambdaQuery()
//                .select(StrategyCatalog::getDestName)
                .groupBy(StrategyCatalog::getDestName)
                .list();
        List<String> destNames = StrategyCatalogs.stream().map(StrategyCatalog::getDestName).collect(Collectors.toList());
        List<CatalogVO> list = new ArrayList<>();
        for (String destName : destNames) {
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("destName",destName);
//            List<StrategyCatalog> catalogList = lambdaQuery().eq(StrategyCatalog::getDestName, destName).list();
//            map.put("catalogList",catalogList);
            CatalogVO catalogVO = new CatalogVO();
            catalogVO.setDestName(destName);
            List<StrategyCatalog> catalogList = lambdaQuery().eq(StrategyCatalog::getDestName, destName).list();
            catalogVO.setCatalogList(catalogList);

            list.add(catalogVO);
        }
        return list;
    }

    @Override
    public List<CatalogVO> groupList() {
        List<CatalogVO> vos = new ArrayList<>();

        QueryWrapper<StrategyCatalog> wrapper = new QueryWrapper<>();
        wrapper.select("dest_name, GROUP_CONCAT(id SEPARATOR \"<#>\") ids, GROUP_CONCAT(name SEPARATOR \"<#>\") names");
        wrapper.groupBy("dest_name");
        List<Map<String, Object>> maps = super.listMaps(wrapper);

        for (Map<String, Object> map : maps) {
            CatalogVO vo = new CatalogVO();
            vo.setDestName(map.get("dest_name").toString());

            String ids = map.get("ids").toString();
            String names = map.get("names").toString();
            List<StrategyCatalog> list = this.toStrategyCatalogList(ids, names);
            vo.setCatalogList(list);

            vos.add(vo);
        }
        return vos;
    }

    private List<StrategyCatalog> toStrategyCatalogList(String ids, String names) {
        List<StrategyCatalog> list = new ArrayList<>();
        String[] idsArrays = ids.split("<#>");
        String[] namesArrays = names.split("<#>");

        for (int i = 0; i < idsArrays.length; i++) {
            StrategyCatalog strategyCatalog = new StrategyCatalog();
            strategyCatalog.setId(Long.valueOf(idsArrays[i]));
            strategyCatalog.setName(namesArrays[i]);

            list.add(strategyCatalog);
        }
        return list;
    }

    @Override
    public List<CatalogVO> groupList1() {
        List<CatalogVO> vos = new ArrayList<>();

        List<StrategyCatalog> list = lambdaQuery().orderByAsc(StrategyCatalog::getDestName).list();
        String destName = list.get(0).getDestName();
        List<StrategyCatalog> scs = new ArrayList<>();
        for (StrategyCatalog sc : list) {
            if (!destName.equals(sc.getDestName())) {
                CatalogVO vo = new CatalogVO();
                vo.setDestName(destName);
                vo.setCatalogList(scs);
                vos.add(vo);

                scs = new ArrayList<>();
                destName = sc.getDestName();
            }
            scs.add(sc);
        }
        CatalogVO vo = new CatalogVO();
        vo.setDestName(destName);
        vo.setCatalogList(scs);
        vos.add(vo);
        return vos;
    }

    @Override
    public List<CatalogVO> groupList2() {
        List<StrategyCatalog> list = lambdaQuery().orderByAsc(StrategyCatalog::getDestName).list();

        return list.stream()
                .collect(Collectors.groupingBy(StrategyCatalog::getDestName))
                .entrySet().stream()
                .map(entry -> {
                    CatalogVO vo = new CatalogVO();
                    vo.setDestName(entry.getKey());
                    vo.setCatalogList(entry.getValue());
                    return vo;
                }).collect(Collectors.toList());
    }

    @Override
    public List<CatalogVO> groupList3() {
        List<StrategyCatalog> list = lambdaQuery().orderByAsc(StrategyCatalog::getDestName).list();

        Map<String, List<StrategyCatalog>> grouped = list.stream()
                .collect(Collectors.groupingBy(StrategyCatalog::getDestName));

        List<CatalogVO> result = new ArrayList<>();

        for (Map.Entry<String, List<StrategyCatalog>> entry : grouped.entrySet()) {
            CatalogVO vo = new CatalogVO();
            vo.setDestName(entry.getKey());
            vo.setCatalogList(entry.getValue());
            result.add(vo);
        }

        Collections.sort(result, (vo1, vo2) -> {
            int cmp = vo1.getDestName().compareTo(vo2.getDestName());
            if (cmp != 0) {
                return cmp;
            } else {
                return vo1.hashCode() - vo2.hashCode();
            }
        });

        return result;
    }

    @Override
    public List<StrategyCatalog> queryCatalogList(Long destId) {
        List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, destId).list();
        for (StrategyCatalog strategyCatalog : list) {
            List<Strategy> strategies = strategyService.queryByCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(strategies);
        }
        return list;
    }
}
